/**
 * 
 */
package array.passed2;

import java.util.Arrays;

/**
 * @author xyyi
 *
 */
public class ThreeSumClosest {

	/**
	Given an array S of n integers, find three integers in S such that the sum 
	is closest to a given number, target. Return the sum of the three integers.
	You may assume that each input would have exactly one solution.

	For example, given array S = {-1 2 1 -4}, and target = 1.

	The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
	 */
	// good
	public int threeSumClosest(int[] num, int target) {
		if (num == null || num.length < 3) {
			return 0;
		}

		Arrays.sort(num);
		int closestSum = 0;
		int minGap = Integer.MAX_VALUE;
		for (int i = 0; i < num.length - 2 && minGap > 0; i++) {
			int start = i + 1;
			int end = num.length - 1;
			while (start < end) {
				int sum = num[i] + num[start] + num[end];
				if (sum == target) {
					minGap = 0;
					closestSum = sum;
					break;
				} else if (sum > target) {
					if (sum - target < minGap) {
						minGap = sum - target;
						closestSum = sum;
					}
					end--;
				} else { // sum < target
					if (target - sum < minGap) {
						minGap = target - sum;
						closestSum = sum;
					}
					start++;
				}
			}
		}

		return closestSum;
	}

	// good
    public int threeSumClosest1(int[] nums, int target) {
        int result = 0;
        int minGap = Integer.MAX_VALUE;
        Arrays.sort(nums);
        for (int i = 0, to = nums.length - 3; i <= to; i++) {
            int left = i + 1, right = nums.length - 1;
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum == target) {
                    return target;
                }
                if (Math.abs(sum - target) < minGap) {
                    minGap = Math.abs(sum - target);
                    result = sum;
                }
                if (sum < target) {
                    left ++;
                }
                else {
                    right--;
                }
            }
        }
        
        return result;
    }	
    
    // best
    public int threeSumClosest2(int[] nums, int target) {
        int result = nums[0] + nums[1] + nums[nums.length - 1];
        Arrays.sort(nums);
        for (int i = 0, to = nums.length - 3; i <= to; i++) {
            int left = i + 1, right = nums.length - 1;
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum == target) {
                    return target;
                }
                if (sum < target) {
                    left ++;
                }
                else {
                    right--;
                }
                if (Math.abs(sum - target) < Math.abs(result - target)) {
                    result = sum;
                }
            }
        }
        
        return result;
    }	
	
	/**
	 * 
	 */
	public ThreeSumClosest() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
